package com.guigu.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.eduservice.entity.Equipment;
import com.guigu.eduservice.entity.PurchaseDemand;
import com.guigu.eduservice.entity.User;
import com.guigu.eduservice.mapper.EquipmentTypeMapper;
import com.guigu.eduservice.mapper.PurchaseDemandMapper;
import com.guigu.eduservice.mapper.UserMapper;
import com.guigu.eduservice.service.PurchaseDemandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.eduservice.vo.PurchaseDemandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 求购信息表（存储买家发布的二手机械求购需求） 服务实现类
 * </p>
 *
 * @author janecraft
 * @since 2025-09-27
 */
@Service
public class PurchaseDemandServiceImpl extends ServiceImpl<PurchaseDemandMapper, PurchaseDemand> implements PurchaseDemandService {

    @Autowired
    private PurchaseDemandMapper purchaseDemandMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private EquipmentTypeMapper equipmentTypeMapper;

    @Override
    public PageInfo<PurchaseDemand> getPurchaseDemandPageList(Integer pageNum, Integer pageSize, PurchaseDemandVo vo) {
        // 分页参数合法性校验（避免无效分页，如pageNum=0、pageSize=-1）
        if (pageNum == null || pageNum < 1) {
            pageNum = 1; // 默认查询第1页
        }
        if (pageSize == null || pageSize < 1 || pageSize > 100) {
            pageSize = 10; // 默认每页10条，最大限制100条（避免性能问题）
        }

        // 启动PageHelper分页（必须在查询SQL执行前调用，且仅对下一次查询生效）
        PageHelper.startPage(pageNum, pageSize);

        // 构建Lambda查询条件（仅在筛选条件非空时添加，避免无效SQL）
        LambdaQueryWrapper<PurchaseDemand> wrapper = new LambdaQueryWrapper<>();
        // 设备类型
        if (vo.getTypeId() != null && vo.getTypeId() != 0){
            wrapper.eq(PurchaseDemand::getTypeId, vo.getTypeId());
        }
        // 买家名称
        if (vo.getContactName() != null && !vo.getContactName().equals("")){
            wrapper.like(PurchaseDemand::getContactName, vo.getContactName());
        }
        List<PurchaseDemand> purchaseDemandList = purchaseDemandMapper.selectList(wrapper);

        for (PurchaseDemand purchaseDemand : purchaseDemandList){
            // 获取用户信息
            purchaseDemand.setUser(userMapper.selectById(purchaseDemand.getUserId()));
            // 获取设备类型信息
            purchaseDemand.setType(equipmentTypeMapper.selectById(purchaseDemand.getTypeId()));
        }

        PageInfo<PurchaseDemand> purchaseDemandPageInfo = new PageInfo<>(purchaseDemandList);
        return purchaseDemandPageInfo;
    }

    @Override
    public List<PurchaseDemand> getPurchaseDemandByUserId(Long userId) {
        if (userId == null){
            return null;
        }
        LambdaQueryWrapper<PurchaseDemand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PurchaseDemand::getUserId, userId);
        List<PurchaseDemand> purchaseDemandList = purchaseDemandMapper.selectList(queryWrapper);
        if (purchaseDemandList!=null){
            for (PurchaseDemand purchaseDemand : purchaseDemandList){
                // 获取设备类型信息
                purchaseDemand.setType(equipmentTypeMapper.selectById(purchaseDemand.getTypeId()));
            }
            return purchaseDemandList;
        }
        return null;
    }

    @Override
    public boolean updatePurchaseDemandByBusinessmanId(Long purchaseDemandId, Long businessmanId) {
        PurchaseDemand purchaseDemand = purchaseDemandMapper.selectById(purchaseDemandId);
        if (purchaseDemand != null){
            purchaseDemand.setBusinessmanId(businessmanId);
            purchaseDemand.setPurchaseStatus(1);
            return purchaseDemandMapper.updateById(purchaseDemand) > 0;
        }
        return false;
    }
}
